<head>
	<title>CS633 Group Project :: Team 1 :: Address Book</title>
	<link rel="stylesheet" href="css/style.css" type="text/css">
</head>
<body>
	<table width="100%">
		<!-- HEADER -->
		<tr style="padding:0px">
			<td><!--#include file ="header.asp"--></td>
		</tr>
		
		<!-- CONTENT -->
		<tr>
			<td align="center">
			<br/>
			
			<%
			'GET FORM VALUES
				newLname = trim(Request("lname"))
				newFname = trim(Request("fname"))
				newAddress = trim(Request("address"))
				newCity = trim(Request("city"))
				newState = trim(Request("state"))
				newZip = trim(Request("zip"))
				newPhone = trim(Request("phone"))
				newEmail = trim(Request("email"))
				
			'CONNECT TO DATABASE
			
				dbPath = "db/contacts.mdb"
			
				Set conn = Server.CreateObject("ADODB.Connection")
				MdbFilePath = Server.MapPath(dbPath)
				conn.Open "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & MdbFilePath & ";"
			
			'INSERT DATA INTO DATABASE
				const BOOLEAN_OP_AND = " AND "
				const BOOLEAN_OP_OR = " OR "
				booleanOp = BOOLEAN_OP_AND
				nef = 0 'Non-empty fields
				
				SQL_stmt = "SELECT * FROM People WHERE "
				
				'Adds where clause
				if len(newLname) > 0 then
					SQL_stmt = SQL_stmt + "LastName LIKE '%"+ newLname +"%'"
					nef = nef + 1 
				end if
			
				if len(newFname) > 0 then
					if nef > 0 then SQL_stmt = SQL_stmt + booleanOp
					SQL_stmt = SQL_stmt + "FirstName LIKE '%"+ newFname +"%'"
					nef = nef + 1
				end if
				
				if len(newAddress) > 0 then
					if nef > 0 then SQL_stmt = SQL_stmt + booleanOp
					SQL_stmt = SQL_stmt + "StreetAddress LIKE '%"+ newAddress +"%'"
					nef = nef + 1
				end if
				
				if len(newCity) > 0 then
					if nef > 0 then SQL_stmt = SQL_stmt + booleanOp
					SQL_stmt = SQL_stmt + "City LIKE '%"+ newCity +"%'"
					nef = nef + 1
				end if
				
				if len(newState) > 0 then
					if nef > 0 then SQL_stmt = SQL_stmt + booleanOp
					SQL_stmt = SQL_stmt + "State LIKE '%"+ newState +"%'"
					nef = nef + 1
				end if
				
				if len(newZip) > 0 then
					if nef > 0 then SQL_stmt = SQL_stmt + booleanOp
					SQL_stmt = SQL_stmt + "ZipCode LIKE '%"+ newZip +"%'"
					nef = nef + 1
				end if
				
				if len(newPhone) > 0 then
					if nef > 0 then SQL_stmt = SQL_stmt + booleanOp
					SQL_stmt = SQL_stmt + "PhoneNo LIKE '%"+ newPhone +"%'"
					nef = nef + 1
				end if
			
				if len(newEmail) > 0 then
					if nef > 0 then SQL_stmt = SQL_stmt + booleanOp
					SQL_stmt = SQL_stmt + "EmailAddress LIKE '%"+ newEmail +"%'"
					nef = nef + 1
				end if
				
				If (nef > 0) Then
					set RS = conn.execute(SQL_stmt)
				
				
			'RENDER PAGE			
				if RS.EOF then
					%>
					<b>No records found!</b><br />
					<%
				else				
				%>		
				<h2>Search Results</h2>			
				<table bgcolor=#990000 width=1000 cellpadding=5 cellspacing=1 border=0>
					<tr class="header">
						<td>
						<td>Last Name
						<td>First Name
						<td>Address
						<td>City
						<td>State
						<td>Zip
						<td>Phone
						<td>E-mail
					<%
						'CYCLE THROUGH RECORDSETS AND DISPLAY THEM
						recordCount = 1
						While NOT RS.EOF
							ID = RS("PersonNo")
							lname = RS("LastName")
							fname = RS("FirstName")
							address = RS("StreetAddress")
							city = RS("City")
							state = RS("State")
							zip = RS("ZipCode")
							phone = RS("PhoneNo")
							email = RS("EmailAddress")
							If (recordCount = 1) Then
								rowColor = "oddrow"
								recordCount = 0
							Else
								rowColor = "evenrow"
								recordCount = 1
							End If
							%>
							<tr class=<%=rowColor%>>
								<td><input type="button" onclick="location.href='entry_details.asp?ID=<%=ID%>'" value="Open">
								<td><%=lname%>
								<td><%=fname%>
								<td><%=address%>
								<td><%=city%>
								<td><%=state%>
								<td><%=zip%>
								<td><%=phone%>
								<td><%=email%>
							<%
							RS.MoveNext
						WEnd
					%>
				</table>
				<%
				end if
				Else
					%>
					<b>No records found!</b><br />
					<%
				End If
				%>
				<br/>
				<input type="button" onclick="history.go(-1);" value="Change Search Parameters" />&nbsp;&nbsp;
				<input type="button" onclick="location.href='index.asp'" value="Home" />				
			</td>
		</tr>
	
		<!-- FOOTER -->
		<tr style="padding:0px">
			<td><!--#include file ="footer.asp"--></td>
		</tr>		
	</table>	
</body>
<%

'CLOSE DATABASE CONNECTION
	conn.close
	set conn = nothing
	set sqlstmt = nothing
%>